package net.bithappens.hnqe.scratchpad;

import java.io.PrintStream;
import java.net.URI;
import java.util.Collection;

import net.bithappens.hnqe.cache.ICacheTraverse;
import net.bithappens.hnqe.graph.GraphNode;
import net.bithappens.hnqe.graph.EntryProperty;
import net.bithappens.hnqe.graph.GraphRelation;
import net.bithappens.hnqe.graph.GraphRelationMember;
import net.bithappens.hnqe.graph.GraphValue;
import net.bithappens.hnqe.impl.cache.CacheManager;

public class TestTraversing {

	public static void main(String[] args) throws Exception {
	
		CacheManager manager = new CacheManager();
		ICacheTraverse traverser = manager;
		
		Thread.sleep(2000);
		
		System.out.println();
		System.out.println();
		
		long start = System.currentTimeMillis();
		GraphNode graphNode = traverser.getNodeByUri("soaf", new URI("http://soaf.com/person/bob"));
		System.out.println("["+ (System.currentTimeMillis() - start) +" ms] query time getNodeByUri");
		
		printGraphNode(System.out, graphNode);
	
		
		System.out.println();
		System.out.println();
			
		start = System.currentTimeMillis();
		GraphRelation graphRelation = traverser.getRelationByUri("soaf", new URI("http://soaf.com/rateServiceWithLevel/1"));
		System.out.println("["+ (System.currentTimeMillis() - start) +" ms] query time getRelationByUri");
		
		printGraphRelation(System.out, graphRelation);
		
		
		
		
		
		System.out.println();
		System.out.println();
			
		start = System.currentTimeMillis();
		Collection<GraphRelation> returnIndicentRelations = traverser.getIncidentRelations("soaf", graphNode);
		System.out.println("["+ (System.currentTimeMillis() - start) +" ms] query time getIncidentRelations");
		
		for(GraphRelation relation : returnIndicentRelations){
			printGraphRelation(System.out, relation);
			System.out.println();
		}
//		
//		
//
//
//		
//		System.out.println();
//		System.out.println();
//			
//		start = System.currentTimeMillis();
//		Collection<GraphRelationMember> graphNodes = traverser.getNeighbors("soaf", graphNode);
//		System.out.println("["+ (System.currentTimeMillis() - start) +" ms] query time getNeighbors");
//		
//		for(GraphRelationMember node : graphNodes){
//			printGraphRelationMember(System.out, node, "|");
//		}

//		
//
//		System.out.println();
//		System.out.println();
//		
//		start = System.currentTimeMillis();
//		Set<GraphRelationMember> entries = new HashSet<GraphRelationMember>();
//		entries.add(graphNode);
//		entries.add(GraphValue.createGraphValue(8));
//		Collection<GraphRelation> returnMatchingRelations = traverser.findRelation("soaf", entries);
//		System.out.println("["+ (System.currentTimeMillis() - start) +" ms] query time findRelation");
//		
//		for(GraphRelation relation : returnMatchingRelations){
//			printGraphRelation(System.out, relation);
//			System.out.println();
//		}
		
		
//		System.out.println();
//		System.out.println();
//		
//		start = System.currentTimeMillis();
//		List<GraphRelationMember> entryList = new LinkedList<GraphRelationMember>();
//		entryList.add(graphNode);
//		entryList.add(new GraphNode(new URI("http://soaf.com/service/datacloud")));
//		entryList.add(GraphValue.createGraphValue(8));
//		Collection<GraphRelation> returnMatchingRelations = traverser.findExactRelation("soaf", entryList);
//		System.out.println("["+ (System.currentTimeMillis() - start) +" ms] query time findExactRelation");
//		
//		for(GraphRelation relation : returnMatchingRelations){
//			printGraphRelation(System.out, relation);
//			System.out.println();
//		}
		
		
		manager.stop();
	}


	private static void printGraphNode(PrintStream out, GraphNode graphNode) {
		printGraphNode(out, graphNode, "|");
	}
	
	
	private static void printGraphNode(PrintStream out, GraphNode graphNode, String prefix) {
		out.println(prefix+"------------------------------------------------------------");
		out.println(prefix+"NodeClass    ["+graphNode.getTypeId()+"]");
		out.println(prefix+"Valid till   ["+graphNode.getLease().getTime()+"]");
		out.println(prefix+"------------------------------------------------------------");
		
		for(EntryProperty property : graphNode.getProperties()){
			out.println(prefix+"+ "+property.getName()+" : "+property.getValue().getValue().toString());
		}
		out.println(prefix+"------------------------------------------------------------");
	}
	
	
	
	private static void printGraphRelation(PrintStream out, GraphRelation graphRelation) {
		out.println("|------------------------------------------------------------");
		out.println("|Relation name  ["+graphRelation.getTypeId()+"]");
		out.println("|Valid till     ["+graphRelation.getLease().getTime()+"]");
		out.println("|------------------------------------------------------------");

		for(EntryProperty property : graphRelation.getProperties()){
			
			out.println("|#"+property.getName()+" : "+ property.getValue().getValue());
			
		}
		
		out.println("|------------------------------------------------------------");
		
		for(GraphRelationMember relationMember : graphRelation){
			
			printGraphRelationMember(out, relationMember, "|>");
			
		}
	}
	
	
	private static void printGraphRelationMember(PrintStream out, GraphRelationMember relationMember, String prefix) {

		if(relationMember instanceof GraphNode){
			out.println(prefix);
			out.println(prefix+"Relation Entry [NODE]");
			printGraphNode(out, (GraphNode) relationMember, "|>|");
		} else {
			out.println(prefix);
			out.println(prefix+"Relation Entry [VALUE]");
			out.println(prefix+"|------------------------------------------------------------");
			out.println(prefix+"|>" + ((GraphValue)relationMember).getEntryValue().getValue());
			out.println(prefix+"|------------------------------------------------------------");
		}
		
	}

}
